package com.demo.zyt.common.utils;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;

import com.sun.mail.util.MailSSLSocketFactory;

import lombok.extern.slf4j.Slf4j;

/**
 */
@Slf4j
public class EmailUtils {

//	@Autowired
//	private Environment env;

	private static String auth = "true";
	private static String host = "smtp.qq.com";
	private static String protocol = "smtp";
	private static int port = 465;
	private static String authName = "t-junwen@foxmail.com";
	private static String password = "fcfpssbrbiqhhchj";
	private static boolean isSSL = true;
	private static String charset = "UTF-8";
	private static String timeout = "5000";

//	@PostConstruct
//    public void initParam () {
//        auth = env.getProperty("mail.smtp.auth");
//        host = env.getProperty("mail.host");
//        protocol = env.getProperty("mail.transport.protocol");
//        port = env.getProperty("mail.smtp.port", Integer.class);
//        authName = env.getProperty("mail.auth.name");
//        password = env.getProperty("mail.auth.password");
//        charset = env.getProperty("mail.send.charset");
//        isSSL = env.getProperty("mail.is.ssl", Boolean.class);
//        timeout = env.getProperty("mail.smtp.timeout");
//    }

	/**
	 * 发送邮件
	 * 
	 * @param subject
	 *            主题
	 * @param toUsers
	 *            收件人
	 * @param ccUsers
	 *            抄送
	 * @param content
	 *            邮件内容
	 */
	@Async
	public static boolean sendEmail(String subject, String[] toUsers, String[] ccUsers, String content) {
		boolean flag = true;
		try {
			JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
			javaMailSender.setHost(host);
			javaMailSender.setUsername(authName);
			javaMailSender.setPassword(password);
			javaMailSender.setDefaultEncoding(charset);
			javaMailSender.setProtocol(protocol);
			javaMailSender.setPort(port);

			Properties properties = new Properties();
			properties.setProperty("mail.smtp.auth", auth);
			properties.setProperty("mail.smtp.timeout", timeout);
			if (isSSL) {
				MailSSLSocketFactory sf = null;
				try {
					sf = new MailSSLSocketFactory();
					sf.setTrustAllHosts(true);
					properties.put("mail.smtp.ssl.enable", "true");
					properties.put("mail.smtp.ssl.socketFactory", sf);
				} catch (GeneralSecurityException e) {
					e.printStackTrace();
				}
			}
			javaMailSender.setJavaMailProperties(properties);

			MimeMessage mailMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
			messageHelper.setTo(toUsers);
			if (ccUsers != null && ccUsers.length > 0) {
				messageHelper.setCc(ccUsers);
			}
			messageHelper.setFrom(authName);
			messageHelper.setSubject(subject);
			
			messageHelper.setText(content, true);
			log.warn("邮件内容:{}",content);
			javaMailSender.send(mailMessage);

		} catch (Exception e) {
			log.error("发送邮件失败!", e);
			flag = false;
		}
		return flag;
	}
}